package com.beifu.manager.service.role.impl;

import com.beifu.manager.dao.RoleMapper;
import com.beifu.manager.dao.RoleModuleMapper;
import com.beifu.manager.dao.UserInfoMapper;
import com.beifu.manager.dao.UserRoleRelationMapper;
import com.beifu.manager.model.*;
import com.beifu.manager.service.role.UserRoleRelationService;
import com.mds.manager.utils.ListUtils;
import com.mds.manager.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beifu.manager.service.role.RoleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 
* Title: RoleServiceImpl
* Description:
* @author    BaiBing
* @date      2018年6月10日
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleRelationMapper userRoleRelationMapper;

    @Autowired
    private RoleModuleMapper roleModuleMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int countByExample(RoleExample example) {
        return roleMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(RoleExample example) {
        return roleMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(int id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void insert(Role record, List<UserRoleRelation> userRoleRelationList, List<RoleModule> roleModuleList) {
        if (userRoleRelationList != null && userRoleRelationList.size() > 0) {
            userRoleRelationMapper.batchInsert(userRoleRelationList);
        }
        roleMapper.insert(record);
        if (roleModuleList != null && roleModuleList.size() > 0) {
            roleModuleMapper.batchInsert(roleModuleList);
        }
    }

    @Override
    public int insertSelective(Role record) {
        return roleMapper.insertSelective(record);
    }

    @Override
    public List<Role> selectByExample(RoleExample roleExample) {
        return roleMapper.selectByExample(roleExample);
    }
    @Override
    public Role selectByPrimaryKey(int id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Role record, RoleExample example) {
        return roleMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(Role record, RoleExample example) {
        return roleMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return roleMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageUtils getRolePage(PageUtils Page) {
        RoleExample example = (RoleExample) Page.getQueryParames();
        List<Role> list = roleMapper.getRolePage((Page.getPage()-1)*Page.getLimit(),Page.getLimit(), example);
        int count = roleMapper.countByExample(null);
        Page.setList(list);
        Page.setCountNum(count);
        return Page;
    }

    @Override
    public int batchDelRole(String[] ids) {
        return roleMapper.batchDelRole(ids);
    }

    @Override
    @Transactional
    public void updateRoleUserPermission(Role record, List<UserRoleRelation> userRoleRelationList, List<RoleModule> roleModuleList) {

        // 先删除角色权限关系，角色用户关系。
        UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
        userRoleRelationExample.createCriteria().andRoleIdEqualTo(record.getId());
        userRoleRelationMapper.deleteByExample(userRoleRelationExample);

        RoleModuleExample roleModuleExample = new RoleModuleExample();
        roleModuleExample.createCriteria().andRoleIdEqualTo(record.getId());
        roleModuleMapper.deleteByExample(roleModuleExample);

        //update userInfo表 roleID
        if (ListUtils.isNotNull(userRoleRelationList)) {
            for (UserRoleRelation userRoleRelation:userRoleRelationList) {
                userRoleRelation.getUserId();
                UserInfo userInfo = new UserInfo();
                userInfo.setId(userRoleRelation.getUserId());
                userInfo.setRoleId(userRoleRelation.getRoleId());
                userInfoMapper.updateByPrimaryKeySelective(userInfo);
            }
        }

        // insert
        if (userRoleRelationList != null && userRoleRelationList.size() > 0) {
            userRoleRelationMapper.batchInsert(userRoleRelationList);
        }
        roleMapper.updateByPrimaryKeySelective(record);
        if (roleModuleList != null && roleModuleList.size() > 0) {
            roleModuleMapper.batchInsert(roleModuleList);
        }
    }

}
